package com.real8.romemp.client.activity;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.real8.romemp.client.ClientFactory;
import com.real8.romemp.client.GameConstants;
import com.real8.romemp.client.place.TradeAllocationPlace;
import com.real8.romemp.client.place.WarPlace;
import com.real8.romemp.client.ui.TradeAllocationView;
import com.real8.romemp.client.ui.TradeAllocationView.Presenter;
import com.real8.romemp.shared.GameData;

public class TradeAllocationActivity extends AbstractActivity implements
		Presenter {
	private final ClientFactory clientFactory;
	private TradeAllocationView view;

	public TradeAllocationActivity(TradeAllocationPlace place,
			ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
	}

	@Override
	public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
		view = clientFactory.getTradeAllocationView();
		if (view != null) {
			view.setPresenter(this);

			// prepare view
			view.hideAll();

			// delay a bit
			Timer timer = new Timer() {
				@Override
				public void run() {
					// determine available ships
					GameData gameData = clientFactory.getGameData();

					// show trade overview
					view.showOverview(WA_availableWarShips(gameData),
							MU_availableMerchantShips(gameData), gameData
									.getLE_labourArtisans());

					// show first Q
					view.showMerchantShipQ();
				}

			};
			timer.schedule(GameConstants.DELAY_6020);

			// set widget
			containerWidget.setWidget(view.asWidget());
		}
	}

	private int MU_availableMerchantShips(GameData gameData) {
		int availableShips = gameData.getLM_labourMerchants();
		if (gameData.getLM_labourMerchants() >= gameData.getMS_merchantShips()) {
			availableShips = gameData.getMS_merchantShips();
		}

		return availableShips;
	}

	private int WA_availableWarShips(GameData gameData) {
		int availableShips = gameData.getWS_warShips();
		if (gameData.getWS_warShips() >= gameData.getLN_labourNavy()) {
			availableShips = gameData.getLN_labourNavy();
		}

		return availableShips;
	}

	@Override
	public void onMerchantShipAnswer(int answer) {
		// set local and oversees merchant ships
		GameData gameData = clientFactory.getGameData();
		if (answer >= 0 && answer <= gameData.getLE_labourArtisans()) {
			gameData.setMO_overseeMerchantShips(answer);
			gameData
					.setML_localMerchantShips(MU_availableMerchantShips(gameData)
							- answer);

			// show next Q
			view.showWarShipQ();
		}
	}

	@Override
	public void onWarShipAnswer(int answer) {
		// set local and oversees war ships
		GameData gameData = clientFactory.getGameData();
		if (answer >= 0 && answer <= WA_availableWarShips(gameData)) {
			gameData.setWO_overseeWarShips(answer);
			gameData.setWL_localWarShips(WA_availableWarShips(gameData)
					- answer);

			// next place
			goToNextPlace();
		}
	}

	private void goToNextPlace() {
		clientFactory.getPlaceController().goTo(new WarPlace());
	}
}
